<?php
/** 充值返回处理程序
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-9-2
 * Time: 下午4:03
 */

use pft\Log\Log;
include '/var/www/html/new/d/class/Log.class.php';
include '/var/www/html/new/d/common/func.inc.php';//recharge_cash_back(充值金额单位元, 会员ID, 支付渠道, $soap);
require("/var/www/html/new/conf/le.je");
$le=new go_sql();
$le->connect();
define('LOG_PATH', '/var/www/html/new/d/module/zax/ips_pay/log/log_recharge.txt');


$ordern = $_GET['billno'];
$amount = $_GET['amount'];
$mydate = $_GET['date'];
$succ = $_GET['succ'];
$msg = $_GET['msg'];
$attach = $_GET['attach'];
$ipsbillno = $_GET['ipsbillno'];
$retEncodeType = $_GET['retencodetype'];
$currency_type = $_GET['Currency_type'];
$signature = $_GET['signature'];

$content = 'billno'.$ordern.'currencytype'.$currency_type.'amount'.$amount.
    'date'.$mydate.'succ'.$succ.'ipsbillno'.$ipsbillno.'retencodetype'.$retEncodeType;
$cert = '6868423594683207055317415846359181319229560680572746757844258714459288782574783'.
    '5274502434575961846130383064183151730364216831296';
$signature_1ocal = md5($content . $cert);
$word = date('Y-m-d H:i:s').'记录开始:|' . json_encode($_GET);
Log::writeLog($word, LOG_PATH);

if ($signature_1ocal == $signature){
    if ($succ == 'Y'){
        //订单号 前面补上了RECHARGE三个字母 表示充值
        $sourceT = 3;
        $ordern = str_replace('RECHARGE','',$ordern);
        $str="select status,total_fee,buyer_email from pft_alipay_rec where out_trade_no='$ordern' AND `sourceT`=$sourceT LIMIT 1";
        $GLOBALS['le']->query($str);
        $pay_log_data = $GLOBALS['le']->fetch_assoc();
        if (!$pay_log_data) {
            $word = '支付log不存在'.$str;
            Log::writeLog($word,LOG_PATH);
            $GLOBALS['le']->close();
            exit('交易失败');
        }
        $status = $GLOBALS['le']->f('status');
        if($status!=0){
            $word ='非正常状态{'.$status.'}';
            Log::writeLog($word,LOG_PATH);
            $GLOBALS['le']->close();
            exit('交易失败');
        }else{
           //回传的信息里单位是元
            $total_fee = $GLOBALS['le']->f("total_fee") ;//数据库中的单位是元
            if ($amount != $total_fee) {
                $word ='支付金额不等。返回金额:' . $amount . ',实际金额:'. $total_fee;
                Log::writeLog($word,LOG_PATH);
                $GLOBALS['le']->close();
                exit('交易失败');
            }
            $sid =$GLOBALS['le']->f('buyer_email');
            $s = new SoapClient(null,array(
                "location" => "http://localhost/open/openService/pft_insideMX.php",
                "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true"));
            //管理员帐号不加钱
            //$s->PFT_Member_Fund_Modify(1, 0, $pay_total_fee, 0, 0,null,3, 0,'','环迅充值');
            //客户帐号加钱
            if(($res=$s->PFT_Member_Fund_Modify($sid,$sid,$total_fee*100,0,0,null,3,0,'','环迅充值'))!=100){
                $word ='客户帐号加钱失败,原因:'.$res;
                Log::writeLog($word,LOG_PATH);
                $GLOBALS['le']->close();
                exit;
            }
            recharge_cash_back($total_fee, $sid, 6, $s);
            $ipsbillno;//环迅唯一交易号
            $str=<<<SQL
UPDATE pft_alipay_rec set status=1,dtime=now(),`trade_no`='$ipsbillno'
WHERE out_trade_no='$ordern' AND `sourceT`=$sourceT limit 1
SQL;
            $GLOBALS['le']->query($str);
            $GLOBALS['le']->close();
            $word ='后台处理完毕';
            Log::writeLog($word,LOG_PATH);
            $GLOBALS['le']->close();
            echo 'ipscheckok';
            exit;
        }
    }else{
        $word = date('Y-m-d H:i:s') . '返回结果为N:支付失败';
        Log::writeLog($word,LOG_PATH);
        echo '支付失败！';
        exit;
    }
}else
{
    $word = date('Y-m-d H:i:s') . '签名不正确!';
    Log::writeLog($word,LOG_PATH);
    echo '签名不正确！';
    exit;
}















